package com.software.project.controller;

import com.software.project.domain.Result;
import com.software.project.entity.StudyTimeRecord;
import com.software.project.service.StudyTimeRecordService;
import com.software.project.utils.LogUtil;
import com.software.project.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/studyTimeRecord")
@Slf4j
public class StudyTimeRecordController {

    @Autowired
    private StudyTimeRecordService studyTimeRecordService;
    @Autowired
    private HttpServletRequest request;

    // 新增学习记录，更新学习记录
    @PostMapping("/createRecord")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    public Result<Double> createRecord(@RequestBody StudyTimeRecord studyTimeRecord) {
        double intelligence = studyTimeRecordService.createRecord(studyTimeRecord);
        log.info(LogUtil.success(request.getRemoteAddr(), studyTimeRecord.toString()));
        return ResultUtil.success(intelligence);
    }

    // 查询近七天的学习记录
    @GetMapping("/findRecord")
    public Result<StudyTimeRecord[]> findRecord(String userId) {
        StudyTimeRecord[] record = studyTimeRecordService.findRecord(userId);
        log.info(LogUtil.success(request.getRemoteAddr(), userId));
        return ResultUtil.success(record);
    }
}
